$(document).ready(function () {

	//-----------------------------------------定义和初始化变量----------------------------------------
	var loadBox = $('aside.loadBox');
	var articleBox = $('article');
	var windowScale = window.innerWidth / 750;

	var indexBox = $("#indexBox")
	var brandBox = $("#brandBox")
	var QAbox = $("#QAbox")
	var uploadBox = $("#uploadBox")
	var rewardBox = $("#rewardBox")
	var rankBox = $("#rankBox")
	var tipsBox = $("#tipsBox")
	var awardListBox = $("#awardListBox")
	var awardTipsBox = $("#awardTipsBox")
	var scoreBox = brandBox.find(".scoreBox")
	var fileInput = $("#imageInput")

	var nowQsIndex = 0;
	var rightNum = 0;
	var score = 0;
	var nowQsData = {};
	var nowChose = null;
	var Finish = 'login'; //是否完成流程
	var nowProcess = 0; //等于0表示都未进行 1-9表示完成第一道到第九道题，10表示已上传，11表示完成抽奖

	var lotteryFlag = true;
	var loadFlag = true;
	var wordScrollFlag = true;

	var imgFiles = [];
	var imgUrls = [];
	var uploadNum = 0;

	var TodayRanks = [];
	var localIds = [];
	var isize=document.documentElement.clientWidth / 750 * 100;

	var awardListScroll = new IScroll('#awardListScroll', {
		bounce: false,
		click: true,
	});
	var rankScroll = new IScroll('#rankScroll', {
		bounce: false,
		click: true,
	});


	//----------------------------------------页面初始化----------------------------------------
	icom.init(init);//初始化
	icom.screenScrollUnable();//如果是一屏高度项目且在ios下，阻止屏幕默认滑动行为

	function init() {
		requestAnimationFrame(function () {
			if (os.screenProp < 0.54) articleBox.addClass("screen189");
			if (os.screenProp <= 0.64 && os.screenProp >= 0.54) articleBox.addClass("screen169");
			if (os.screenProp > 0.64) articleBox.addClass("screen159");
			// localStorage.clear()
			getUserProcess(function () {
				load_handler();
			});
		});
		wxUser.init({
			shareInfo: {
				title: "宝，粉我！",
				friend: '即刻开启丝芙兰探索之旅',
				timeline: '宝，粉我！'
			}
		});
	}//edn func


	//----------------------------------------加载页面图片----------------------------------------
	function load_handler() {
		if (loadFlag) {
			loadFlag = false
			var loader = new PxLoader();
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/share.jpg');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/uploadBox/code.jpg');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/uploadBox/cont.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/rewardBox/awardBtn.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/rewardBox/bg.jpg');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/rewardBox/c1.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/rewardBox/c2.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/rewardBox/c3.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/rewardBox/rankBtn.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/rewardBox/shadow.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/rankBox/bg.jpg');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/rankBox/cont.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/rankBox/f.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/rankBox/p.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/rankBox/plus.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/rankBox/t1.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/rankBox/t2.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/rankBox/t3.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/QAbox/c.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/QAbox/e.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/QAbox/k.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/QAbox/n.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/QAbox/ok.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/public/heart.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/public/heart2.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/public/star.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/public/title.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums2/0.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums2/1.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums2/2.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums2/3.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums2/4.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums2/5.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums2/6.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums2/7.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums2/8.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums2/9.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums/0.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums/1.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums/2.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums/3.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums/4.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums/5.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums/6.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums/7.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums/8.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/nums/9.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i1.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i1a.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i1b.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i1s.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i2.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i2a.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i2b.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i2s.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i3.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i3a.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i3b.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i3s.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i4.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i4a.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i4b.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i4s.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i5.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i5a.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i5b.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i5s.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i6.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i6a.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i6b.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i6s.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i7.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i7a.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i7b.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i7s.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i8.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i8a.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i8b.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i8s.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i9.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i9a.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i9b.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/logo/i9s.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/indexBox/bg.jpg');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/indexBox/btn.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/indexBox/k.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/indexBox/logo.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/indexBox/p.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/indexBox/title.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/common/bgm_off.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/common/bgm_on.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/common/close.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/common/turn_lock.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/common/turn_no.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/common/turn_phone.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/common/turn_unlock.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/common/turn_yes.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/brandBox/b.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/brandBox/bar.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/brandBox/bg.jpg');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/brandBox/bg2.jpg');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/brandBox/book.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/brandBox/box.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/brandBox/c.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/brandBox/c2.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/brandBox/heart.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/brandBox/k.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/brandBox/light.png');
			loader.addImage('https://cdn.dhteam.net/sephora/h5/images/brandBox/play.png');

			loader.addImage('images/awardList/t.png');
			loader.addImage('images/awardList/1.png');
			loader.addImage('images/awardList/2.png');
			loader.addImage('images/awardList/3.png');

			loader.addCompletionListener(function () {
				icom.fadeIn(articleBox);
				pageInit();
				loader = null;
			});
			loader.start();
		}
	}//end func

	//----------------------------------------页面逻辑代码----------------------------------------

	/**
	 * 页面初始化
	 */
	function pageInit() {
		eventInit();
		// DevelopTest();
		jaudgeShowPage(false);
		monitor_handler();
	}//end func

	/**
	 * 开发测试使用
	 */
	function DevelopTest() {
		indexBox.hide();
		awardListBox.show();
		// awardListBox.show();
		getAwardList();
		// awardListBox.show();
		// showBrandBox()
		// showRewardBox();
		// wordScrollAni(['aaaa','bbbbb','cccc','dddd','eeeee']);
	}

	/**
	 * 事件初始化
	 */
	function eventInit() {
		$(".limitBtn").on("touchend", limitClick);

		$("#cardNo").blur(getUserName)
		// fileInput.on('change', file_select);
		// $("#uploadBtn").on('touchend', function () {
		// 	fileInput.click()
		// })
		$("#uploadBtn").on('touchend', getImgFile)

		indexBox.find(".loginBtn").on("touchend", vefForm)
		brandBox.find(".startBtn").on("touchend", showQaBox)

		QAbox.on("touchend", '.answer', choseAnswer)
		QAbox.find(".okBtn").on("touchend", confirmAnswer)

		$("#awardBtn").on("touchend", showAwardListBox)
		$("#rankBtn").on("touchend", showRankBox)
		$("#lotteryBtn").on("touchend", lottery)

		awardListBox.find(".rankBtn").on("touchend", switchRankBox)
		rankBox.find(".awardBtn").on("touchend", switchAwardListBox)
	}

	/**
	 * 显示页面
	 */
	function switchAwardListBox() {
		icom.fadeOut(rankBox)
		icom.fadeIn(awardListBox)
		awardListScroll.refresh();
	}

	/**
	 * 显示页面
	 */
	function switchRankBox() {
		icom.fadeOut(awardListBox)
		icom.fadeIn(rankBox)
		rankScroll.refresh();
	}

	/**
	 * 显示页面
	 */
	function showRankBox() {
		icom.fadeOut(rewardBox)
		icom.fadeIn(rankBox)
		wordScrollAni(TodayRanks)
		rankScroll.refresh();
	}

	/**
	 * 显示页面
	 */
	function showAwardListBox() {
		icom.fadeOut(rewardBox)
		icom.fadeIn(awardListBox)
		awardListScroll.refresh();
	}

	/**
	 * 选择文件
	 */
	function file_select() {
		var files = this.files;
		imgFiles = files
		if (files.length > 9) {
			icom.alert('最多上传9张')
			return
		}
		loadBox.show();
		if (files.length > 0) {
			for (let i = 0; i < files.length; i++) {
				uploadImg(files[i])
			}
		}//end if
		else loadBox.hide();
	}//end func

	/**
	 * 获取图片
	 */
	function getImgFile(){
		// icom.fadeIn(loadBox)
		wx.chooseImage({
			count: 9, // 默认9
			sizeType: ['compressed'],//'original'
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: function (res) {
				// console.log(res.localIds)
				localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
				uploadImg2(0)
			}
		});
	}

	/**
	 * 上传图片
	 */
	 function uploadImg2(i) {
		loadBox.show();
		wx.uploadImage({
			localId: localIds[i], // 需要上传的图片的本地ID，由chooseImage接口获得
			isShowProgressTips: 0, // 默认为1，显示进度提示
			success: function (res) {
			  var serverId = res.serverId; // 返回图片的服务器端ID
			  imgUrls.push(serverId)
			  if(imgUrls.length >= localIds.length){
				updateImg();
			  }
			  else{
				uploadImg2(i+1)
			  }
			  console.log('serverId:'+serverId)
			}
		});
	}

	/**
	 * 上传图片
	 */
	function uploadImg(img) {
		var formData = new FormData();
		formData.append("file", img)
		API.UploadSFL(formData, function (res) {
			if (res) {
				imgUrls.push(res.file)
				uploadNum++
				if (uploadNum >= imgFiles.length) {
					updateImg()
				}
			}
		})
	}

	/**
	 * 更新上传图片
	 */
	function updateImg() {
		loadBox.show();
		var data = {
			src: ""
		}
		for (let i = 0; i < imgUrls.length; i++) {
			const element = imgUrls[i];
			data.src += (i == 0 ? '' : '|') + element 
		}
		API.subpicture(data, function (res) {
			if (res.Status == 'ok') {
				loadBox.hide()
				showRewardBox()
			}
		})
	}

	/**
	 * 获取题目
	 */
	function getQuestions() {
		API.GetQuestions(function (res) {
			if (res.Status == 'ok') {
				dealQuestions(res.Tag.list)
			}
		})
	}

	/**
	 * 处理题目
	 */
	function dealQuestions(list) {
		let arr = []
		let sybom = ['A．', 'B．', 'C．']
		for (let i = 0; i < list.length; i++) {
			const element = list[i];
			let item = {
				id: parseInt(element.key),
				score: 20,
				// intro: qsData[i].intro,
				intro: element.intro,
				// question: element.key + '．' + element.value,
				question: element.value,
				answers: [],
				correct: 0
			}
			for (let j = 0; j < element.options.length; j++) {
				const ele = element.options[j];
				// item.answers.push(sybom[j] + ele.opt_title)
				item.answers.push(ele.opt_title)
				if (ele.isright) item.correct = j
			}
			arr.push(item)
		}
		qsData = arr
	}

	/**
	 * 获取排行部列表
	 */
	function getRankList() {
		API.getrank(function (res) {
			if (res.Status == 'ok') {
				renderRankList(res.Tag)
			}
		})
	}

	/**
	 * 渲染排行榜页面
	 */
	function renderRankList(data) {
		var benchmark = data.Ranks[0].Total
		TodayRanks = data.TodayRanks

		renderSccore($("#selfScore"),data.MyPoints)
		renderSccore($("#otherScore"),data.MyOtherPoints)
		
		let cont = ''
		for (let i = 0; i < data.Ranks.length; i++) {
			const element = data.Ranks[i];
			const nums = (element.Total+'').split('')
			let numHtml = '';
			for (let j = 0; j < nums.length; j++) {
				numHtml += '<img src="https://cdn.dhteam.net/sephora/h5/images/nums2/'+nums[j]+'.png">'
			}
			numHtml += '<img src="https://cdn.dhteam.net/sephora/h5/images/rankBox/f.png">'
			cont += `<div class="block"><div class="word"><div class="wordImg">${numHtml}</div><span style="left:-${nums.length * 0.08}rem;">${element.Area}</span></div>
			<div class="barBox">
				<div class="bar" style="width:${element.Total/benchmark*100}%;"></div>
			</div></div>`
		}
		$("#rankListBox").html(cont)
	}

	/**
	 * 渲染分数
	 */
	function renderSccore(box,score){
		let cont = '<img src="https://cdn.dhteam.net/sephora/h5/images/rankBox/plus.png">'
		let str = (score + '').split('')
		for (let i = 0; i < str.length; i++) {
			cont += '<img src="https://cdn.dhteam.net/sephora/h5/images/nums/'+str[i]+'.png">'
		}
		box.html(cont)
	}

	/**
	 * 获取中奖记录
	 */
	function getAwardList() {
		// renderAwardList({})
		API.getcharge(function (res) {
			if (res.Status == 'ok') {
				renderAwardList(res.Tag)
			}
		})
	}

	/**
	 * 渲染中奖记录页面
	 * @param {*} data 
	 */
	function renderAwardList(data) {
		var cont = ''
		var count = 1
		var lens = [0,1,10,18]
		var showData = {
			'一等奖':[],
			'二等奖':[],
			'三等奖':[]
		}
		for (const key1 in data) {
			for (const key2 in showData) {
				if(key1 === key2){
					showData[key2] = data[key1]
				}
			}
		}
		
		for (const key in showData) {
			cont += `<div class="block">
			<img src="images/awardList/${count}.png" class="t">
			<div class="box">`
			let len = showData[key].length > lens[count] ? showData[key].length : lens[count]
			for (let i = 0; i < len; i++) {
				const element = showData[key][i];
				cont += `<div class="item ${len == 1 ? 'sp' : ''}">${element ? element.Uid : ''}</div>`
			}
			cont += `</div></div>`
			count++
		}
		$("#awardList").html(cont)
	}

	/**
	 * 判断现在显示的页面
	 */
	function jaudgeShowPage(ani) {
		if (Finish == 'login') {
			return
		}
		else {
			getQuestions();
			getRankList();
			getAwardList();
		}
		if (Finish) {
			ani ? icom.fadeOut(indexBox) : indexBox.hide()
			icom.fadeIn(rankBox)
			setTimeout(function(){
				wordScrollAni(TodayRanks)
				rankScroll.refresh();
			},1000)
		}
		else {
			if (nowProcess < 9) showBrandBox();
			else if (nowProcess == 9) {
				ani ? icom.fadeOut(indexBox) : indexBox.hide()
				icom.fadeIn(uploadBox)
			}
			else if (nowProcess == 10) {
				ani ? icom.fadeOut(indexBox) : indexBox.hide()
				icom.fadeIn(rewardBox)
			}
			else {
				ani ? icom.fadeOut(indexBox) : indexBox.hide()
				icom.fadeIn(rankBox)
				setTimeout(function(){
					wordScrollAni(TodayRanks)
					rankScroll.refresh();
				},1000)
			}
		}
	}

	/**
	 * 获取用户进度
	 */
	function getUserProcess(callback) {
		API.GetCurrentProcess(function (res) {
			if (res.Status == 'ok') {
				Finish = res.Tag.Finish
				nowProcess = res.Tag.ProcessIndex
				score = res.Tag.CurrentPoints
				scoreBox.text(score)
				// Finish = 0
				// nowProcess = 9
				if (nowProcess < 9) nowQsIndex = nowProcess
			}
			if(callback) callback();
		})
		setTimeout(function () {
			if(callback) callback();
		}, 1000)
	}

	/**
	 * 获取用户名
	 */
	function getUserName() {
		API.GetUserName({ userid: $("#cardNo").val() }, function (res) {
			if (res.Status == 'ok') {
				var names = res.Tag.UserName.split("*")
				$("#lastName").val(names[0])
				if (res.Tag.UserName.length > 2) $("#firstName").val(names[1])
			}
		})
	}

	/**
	 * 文字滚动
	 */
	function wordScrollAni(arr) {
		if (wordScrollFlag && arr.length > 0) {
			wordScrollFlag = false

			var scrollBox = rankBox.find(".dataBox .scrollBox");
			var scrollBox2 = rankBox.find(".dataBox .scrollBox2");
			var cont = ''
			for (let i = 0; i < arr.length; i++) {
				cont+= '<div class="item">'+arr[i].Area + ' +' + arr[i].Total + '分</div>'
				
			}
			scrollBox.html(cont)
			scrollBox2.html(cont)

			let dis = parseInt(scrollBox.css("width"))
			wordAni(scrollBox,isize*5.6)
			wordAni(scrollBox2,isize*5.6+dis)

			function wordAni(box,num) {
				box.css({"x":num})
				box.transition({ x: -dis }, (num+dis)/0.06, 'linear', function () {
					wordAni(box,dis)
				})
			}
		}
	}

	/**
	 * 显示问答页面
	 */
	function showQaBox() {
		if (nowQsIndex < qsData.length) {
			nowChose = null
			renderQaBox()
			icom.fadeOut(brandBox)
			icom.fadeIn(QAbox)
		}
	}

	/**
	 * 选择答案
	 */
	function choseAnswer() {
		nowChose = parseInt($(this).data("val"));
		var answer = QAbox.find(".answer")
		answer.find(".choseA").hide();
		answer.eq(nowChose).find(".choseA").show();
	}

	/**
	 * 显示提示
	 * @param {*} word 
	 * @param {*} time 
	 */
	function showTips(word, time) {
		var hideTime = time || 2000
		tipsBox.find(".tips").text(word)
		icom.fadeIn(tipsBox)
		setTimeout(function () {
			icom.fadeOut(tipsBox)
		}, hideTime)
	}

	/**
	 * 确认答案
	 */
	function confirmAnswer() {
		if (nowChose == null || nowChose == 'null') {
			icom.alert("请选择答案")
			return
		}
		QAbox.find(".answer .word").removeClass("lighting3")
		if (nowChose == nowQsData.correct) {
			showTips('回答正确')
			rightNum++
			submitQA(1);
		}
		else {
			showTips('回答错误')
			QAbox.find(".answer").eq(nowQsData.correct).find(".word").addClass("lighting3")
			QAbox.find(".answer").eq(nowChose).find(".choseA").show().attr('src', 'https://cdn.dhteam.net/sephora/h5/images/QAbox/e.png')
			QAbox.find(".answer").eq(nowQsData.correct).find(".choseA").show().attr('src', 'https://cdn.dhteam.net/sephora/h5/images/QAbox/c.png')
			submitQA(0);
		}

		if (nowQsIndex < qsData.length) nowQsIndex++
		setTimeout(function () {
			backToBrandBox();
		}, 2000)
	}

	/**
	 * 提交答案
	 */
	function submitQA(answer) {
		var data = {
			key: nowQsData.id,
			isright: answer
		}
		API.SubQuestion(data, function (res) {
			if (res.Status == 'ok') {
				score = res.Tag.CurrentPoints
				scoreBox.text(score)
			}
		})
	}

	/**
	 * 返回品牌页面
	 */
	function backToBrandBox() {
		renderBrandBox(true);
		icom.fadeIn(brandBox)
		if (nowQsIndex >= qsData.length) {
			setTimeout(function () {
				showAwardTips();
			}, 2000)
		}
	}

	/**
	 * 显示中奖提示
	 */
	function showAwardTips() {
		icom.fadeIn(awardTipsBox);
		setTimeout(function () {
			awardTipsBox.find(".starTitle").transition({ y: 0 }, 800, function () {
				icom.fadeOut(awardTipsBox)
				showUploadBox();
			})
		}, 2000)
	}

	/**
	 * 显示上传图片页面
	 */
	function showUploadBox() {
		icom.fadeIn(uploadBox)
		icom.fadeOut(brandBox)
	}

	/**
	 * 显示抽奖页面
	 */
	function showRewardBox() {
		icom.fadeIn(rewardBox)
		icom.fadeOut(uploadBox)
		// lottery(5)
	}

	/**
	 * 抽奖
	 * @param {*} award 
	 */
	function lottery() {
		if (lotteryFlag) {
			lotteryFlag = false
			API.Lottery(function (res) {
				if (res.Status == 'ok') {
					rewardBox.addClass("noPointer")
					turntableAnime(res.Tag.key + '')
					getRankList();
					setTimeout(function () {
						icom.alert("恭喜你获得" + res.Tag.val, showRankBox)
					}, 5000)
				}
				else {
					icom.alert(res.Msg)
				}
			})
			// turntableAnime(6)
		}
	}

	/**
	 * 转盘动画
	 * @param {*} award 1 一等奖 2 二等奖 3 三等奖 4 10积分 5 30积分 6 谢谢参与
	 */
	function turntableAnime(award) {
		var table = rewardBox.find('.circle3');
		let awawrdKey = { "1": 0, "2": 4, "3": 2, "4": 5, "5": 3, "6": 1 }
		TweenMax.to(table, 4, { rotation: 360 * 5 + (60 * awawrdKey[award]) + "deg", ease: Power2.easeInOut });
	}//end func

	/**
	 * 渲染问答页面
	 */
	function renderQaBox() {
		nowQsData = qsData[nowQsIndex];
		var logo = QAbox.find(".logo");
		var logoBottom = QAbox.find(".logoBottom");
		var questionBox = QAbox.find(".questionBox");
		var introWord = QAbox.find(".introBox .word");
		var answerBox = QAbox.find(".answerBox");
		logo[0].src = `https://cdn.dhteam.net/sephora/h5/images/logo/i${nowQsData.id}s.png`
		logoBottom[0].src = `https://cdn.dhteam.net/sephora/h5/images/logo/i${nowQsData.id}b.png`
		var question = nowQsData.question.split("．")
		questionBox.html(`<div>${nowQsData.id}.</div><div>${question}</div>`)
		introWord.html(nowQsData.intro)

		var cont = ''
		var keys = ['A','B','C']
		for (let i = 0; i < nowQsData.answers.length; i++) {
			const element = nowQsData.answers[i].split("．");
			cont += `<div class="answer" data-val="${i}">
						<div class="word"><div>${keys[i]}.</div><div>${element}</div></div>
						<img src="https://cdn.dhteam.net/sephora/h5/images/QAbox/n.png" class="chose">
						<img src="https://cdn.dhteam.net/sephora/h5/images/QAbox/c.png" class="chose choseA none">
					</div>`
		}
		answerBox.html(cont)
	}

	/**
	 * 显示品牌页面
	 */
	function showBrandBox() {
		icom.fadeIn(brandBox)
		icom.fadeOut(indexBox)
		renderBrandBox(false);
		brandAnime();
	}

	/**
	 * 渲染品牌页面
	 */
	function renderBrandBox(anime) {
		if (nowQsIndex < qsData.length) {
			nowQsData = qsData[nowQsIndex]
			brandBox.find(".logoBottom")[0].src = `https://cdn.dhteam.net/sephora/h5/images/logo/i${nowQsData.id}b.png`
			brandBox.find(".tips").text("STAGE " + (nowQsIndex + 1))
		}
		scoreBox.text(score)

		if (anime) {
			icom.fadeOut(QAbox, 500, function () {
				brandBox.find(".logos.act .logo").eq(nowQsIndex - 1).transition({ opacity: 1 }, 800)
			})
		}
		else {
			for (let i = 0; i < nowQsIndex; i++) {
				brandBox.find(".logos.act .logo").eq(i).css({ opacity: 1 })
			}
		}
	}

	/**
	 * 品牌动画
	 */
	function brandAnime() {
		var bookCover = brandBox.find(".cover");
		var pattern1 = brandBox.find(".pattern1");
		var pattern2 = brandBox.find(".pattern2");
		var pattern3 = brandBox.find(".pattern3");
		var pattern4 = brandBox.find(".pattern4");
		var tips = brandBox.find(".tips");
		var cont = brandBox.find(".cont");
		var logoBottom = brandBox.find(".logoBottom");
		var bg1 = brandBox.find(".bg1");
		var bookBox = brandBox.find(".bookBox");

		bookCover.addClass('bookAnime')

		setTimeout(function () {
			icom.fadeOut(bg1, 500)
			icom.fadeOut(pattern1, 500)
			icom.fadeOut(pattern2, 500)
			icom.fadeIn(pattern3, 500)
			icom.fadeIn(pattern4, 500)
			icom.fadeIn(tips, 500)
			icom.fadeIn(scoreBox, 500)
			icom.fadeIn(logoBottom, 500)
			cont.removeClass('noPointer')
			bookBox.hide();
		}, 2000)
	}

	/**
	 * 验证表单
	 */
	function vefForm() {
		var id = $("#cardNo").val();
		var name = $("#name").val();

		if (id == '') icom.alert('请输入ID号码')
		else if (name == '') icom.alert('请输入验证名字')
		else {
			submitInfo(id, name);
		}
	}

	/**
	 * 提交信息
	 * @param {*} id 
	 * @param {*} name 
	 */
	function submitInfo(id, name) {
		$("input").blur();
		var data = {
			userid: id,
			name: $("#lastName").val() + name + ($("#firstName").val() || '')
		}
		var flag = true;
		API.Login(data, function (res) {
			if (res.Status == 'ok') {
				localStorage.setItem('loginToken', res.Tag.Token)
				getUserProcess(function () {
					if (flag) {
						flag = false;
						jaudgeShowPage(true);
					}
				})
			}
			else {
				icom.alert(res.Msg)
			}
		})
	}

	/**
	 * 限制点击
	 */
	function limitClick() {
		$(".limitBtn").addClass('noPointer');
		setTimeout(function () { $(".limitBtn").removeClass('noPointer') }, 500);
	}//end func

	//----------------------------------------页面监测代码----------------------------------------
	function monitor_handler() {
		//		imonitor.add({obj:$('a.btnTest'),action:'touchstart',category:'default',label:'测试按钮'});
	}//end func
});//end ready
